#include "bmp_encoder.h"
#include <stdio.h>
#include <time.h>
#include "USER_CFG.h"
#include "v4a_hal.h"
#include <string.h>
#include "spi.h"
#include <stdlib.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/socket.h>
#include <netinet/in.h>

int date_config_flag=0;
int time_config_en=0;
int date_config_en=0;
int real_offset=0;
int time_offset=0;
int date_offset=0;
int date_change=0;
int time_change=0;
int cur_bit=0;

struct tm * sys_time;
int tm_cfg = 0;

extern IMGNODE bkgnd_fbcopy;
extern IMGNODE bkgnd_fbdisplay;
extern IMGNODE overlay_fbcopy;
extern IMGNODE overlay_fbdisplay;
extern IMGNODE flex_fbdisplay;

extern unsigned int GUI_LCM_XMAX,GUI_LCM_YMAX; 
extern REGION G_REG_DIR;
extern REGION G_REG_FLR;
extern REGION G_REG_TIME;
extern REGION G_REG_DAY;
extern REGION G_REG_WEEK;
extern REGION G_REG_ALARM;
extern REGION G_REG_BKGND;
extern REGION G_REG_LOGO;
extern REGION G_REG_OTHER;
extern REGION G_REG_VIDEO;
extern REGION G_REG_TXT;
extern IMGNODE ptr;


extern unsigned int G_LFTTXT_txt_vfmode;
extern unsigned int G_LFTTXT_vf_speed;
extern unsigned int G_LFTTXT_txt_color;

_enum_DMODE G_DispMode;

IMGNODE SYS_TIME;
IMGNODE SYS_DAY;
IMGNODE SYS_WEEK;
IMGNODE SYS_DIR;
IMGNODE SYS_FLR;
IMGNODE SYS_STE;
IMGNODE SYS_LOGO;

IMGNODE display_char;
IMGNODE temp_display_char;


int Dt_last_year=0;
int Dt_last_month=0;
int Dt_last_day=0;
int Dt_last_weekday = -1;

//int display_char_buffer[1280*1024];
char MD5Code0[40];

extern char userbkgnd_bmp[4*1366*768];
extern char userbkgnd_bmp_data[1366*768*4];
IMGNODE userbkgnd_pic;
IMGNODE userbkgnd_pic0;

REGION Region[11];
extern REGION DisplayRegion[11];

extern Flr_Map flr_map[255];
int flr_map_flag = 0;

char * pDir_logo[4] =
{
	"/v4asys/LIFT/LIFT_0C/BKGND/FLR_BSD/logo.bmp",
	"/v4asys/LIFT/LIFT_90C/BKGND/FLR_BSD/logo.bmp",
	"/v4asys/LIFT/LIFT_180C/BKGND/FLR_BSD/logo.bmp",
	"/v4asys/LIFT/LIFT_270C/BKGND/FLR_BSD/logo.bmp",
};

char * Userbkgnd_Path[4] =
{
	"/v4asys/LIFT/LIFT_0C/BKGND/FLR_BSD/",
	"/v4asys/LIFT/LIFT_90C/BKGND/FLR_BSD/",
	"/v4asys/LIFT/LIFT_180C/BKGND/FLR_BSD/",
	"/v4asys/LIFT/LIFT_270C/BKGND/FLR_BSD/",
};

char * filealarm[] =
{
	"attendant_0.bmp",	//0：司机
	"attendant_1.bmp",
	"Fullload_0.bmp",	//1：满载
	"Fullload_1.bmp",
	"overload_0.bmp",	//2：超载
	"overload_1.bmp",
	"fire_0.bmp",		//3：消防		
	"fire_1.bmp",	
	"inspection_0.bmp",	//4：检修
	"inspection_1.bmp",
	"independent_0.bmp",	//5：专用
	"independent_1.bmp",	
	"fault_0.bmp",		//6：故障
	"fault_1.bmp",	
	"lock_0.bmp",		//7：锁梯
	"lock_1.bmp"
				//8：无
};

char* weekday[7]=
{
	"Sunday.bmp",
	"Monday.bmp",
	"Tuesday.bmp",
	"Wednesday.bmp",
	"Thursday.bmp",
	"Friday.bmp",
	"Saturday.bmp"
};

char * DirFile_U[] =
{
	"dir_U0.bmp",
	"dir_U1.bmp",
	"dir_U2.bmp",
	"dir_U3.bmp",
	"dir_U4.bmp",
	"dir_U5.bmp",
	"dir_U6.bmp",

};

char * DirFile_D[] =
{
	"dir_D0.bmp",
	"dir_D1.bmp",
	"dir_D2.bmp",
	"dir_D3.bmp",
	"dir_D4.bmp",
	"dir_D5.bmp",
	"dir_D6.bmp",
};

char * Appease_picture[] =
{
	"1.jpg",
	"2.jpg",
	"3.jpg",
	"4.jpg",
	"5.jpg",
	"6.jpg"
};

/*******************************************************
 * 根据角度初始化界面参数
*******************************************************/

void InitialGUI(void)
{
	unsigned int i;

	Region[0] = G_REG_DIR;
	Region[1] = G_REG_FLR;
	Region[2] = G_REG_TIME;
	Region[3] = G_REG_DAY;
	Region[4] = G_REG_WEEK;
	Region[5] = G_REG_ALARM;
	Region[6] = G_REG_BKGND;
	Region[7] = G_REG_OTHER;
	Region[8] = G_REG_TXT;
	Region[9] = G_REG_VIDEO;
	Region[10] = G_REG_LOGO;
	
	if(G_DispMode == dmode_0C)
	{
		for(i=0;i<11;i++)
		{
			DisplayRegion[i].x_len = Region[i].x_len;
			DisplayRegion[i].y_len = Region[i].y_len;			
			DisplayRegion[i].s_x = Region[i].s_x;
			DisplayRegion[i].s_y = Region[i].s_y;

		}
	}
	else if(G_DispMode == dmode_90C)
	{
		for(i=0;i<11;i++)
		{
			DisplayRegion[i].x_len = Region[i].y_len;
			DisplayRegion[i].y_len = Region[i].x_len;			
			
			DisplayRegion[i].s_x = GUI_LCM_XMAX - Region[i].s_y - Region[i].y_len;
			DisplayRegion[i].s_y = Region[i].s_x;

		}

	}
	else if(G_DispMode == dmode_180C)
	{
		for(i=0;i<11;i++)
		{
			DisplayRegion[i].x_len = Region[i].x_len;
			DisplayRegion[i].y_len = Region[i].y_len;			
			DisplayRegion[i].s_x = GUI_LCM_XMAX - Region[i].s_x - Region[i].x_len;
			DisplayRegion[i].s_y = GUI_LCM_YMAX - Region[i].s_y - Region[i].y_len;
		}
	}
	else if(G_DispMode == dmode_270C)
	{
		for(i=0;i<11;i++)
		{
			DisplayRegion[i].x_len = Region[i].y_len;
			DisplayRegion[i].y_len = Region[i].x_len;						
			DisplayRegion[i].s_x = Region[i].s_y;
			DisplayRegion[i].s_y = GUI_LCM_YMAX - Region[i].s_x - Region[i].x_len;
		}
	}
	
}



/*******************************************************
 * 显示时间 2
*******************************************************/
unsigned char DisplayTime(void)
{
	unsigned char dispbuf[10];
	unsigned char blink_flag[10];
	unsigned int temp;
	unsigned int index;

	GUIRECT rect;
	GUIPOINT sp;
	BMPINFO *info;
	BMPINFO infotemp;
	unsigned int i;
	char filename[64];
	char tempname[16];
	unsigned char flag_error;
	unsigned int width[10];
	unsigned int height[10];
	unsigned int sum_width,sum_height;

	unsigned char * fileptr;
	unsigned char index_file;
	char buf[100];

	time_t timer =time(NULL);
	sys_time = localtime(&timer);

	info = &infotemp;

	if((DisplayRegion[2].x_len == 0) || (DisplayRegion[2].y_len == 0))
	{
		return 0;
	}

	//如果没有变化，则不更新显示
	
	for(index=0;index<8;index++)
	{
		dispbuf[index] = 0;
		blink_flag[index] = 0;
		width[index] = 0;
		height[index] = 0;
	}

	temp = sys_time->tm_hour / 10;
	dispbuf[0] = '0' + temp;
	temp = sys_time->tm_hour % 10;
	dispbuf[1] = '0' + temp;

	if((sys_time->tm_sec % 2) == 0)
	{
		dispbuf[2] = ':';
	}
	else
	{
		dispbuf[2] = ' ';
	}

	temp = sys_time->tm_min / 10;
	dispbuf[3] = '0' + temp;
	temp = sys_time->tm_min % 10;
	dispbuf[4] = '0' + temp;


	if(date_config_flag==1)
		dispbuf[2] = ':';

	if(time_config_en==1)
	{
		dispbuf[time_offset]=dispbuf[time_offset]+time_change;
			
		if( dispbuf[time_offset] > '9')
			dispbuf[time_offset]='0';
		else if( dispbuf[time_offset] < '0')
			dispbuf[time_offset]='9';

		if((dispbuf[0]-'0')*10+dispbuf[1]-'0'>=24)
		{
			dispbuf[0]='2';
			dispbuf[1]='0';
		}

		if((dispbuf[3]-'0')*10+dispbuf[4]-'0'>=60)
		{
			dispbuf[3]='5';
			dispbuf[4]='0';
		}

		if(time_change!=0)
		{
		memset(buf,0,100);
		sprintf(buf,"date %d.%d.%d-%c%c:%c%c",sys_time->tm_year+1900,sys_time->tm_mon+1,sys_time->tm_mday,dispbuf[0],dispbuf[1],dispbuf[3],dispbuf[4]);
		system(buf);
		system("hwclock -w");
		time_change=0;
		}
		if(cur_bit)
			blink_flag[time_offset]=1;
			
	}

	SYS_TIME.img_base = 8*1024*1024;
	SYS_TIME.img_xlen = DisplayRegion[2].x_len;
	SYS_TIME.img_ylen = DisplayRegion[2].y_len;
	SYS_TIME.img_bpp = 32;
	v4a_drawrect(&SYS_TIME,gui_rect(0,0,DisplayRegion[2].x_len,DisplayRegion[2].y_len),ARGB(0,0,0,0));
	
	index = 0;
	while(index < 5)
	{
		if(dispbuf[index] == 0)
		{
			break;
		}
				
		sprintf(filename,"%s","");
		sprintf(tempname,"0x%x.bmp",dispbuf[index]);
		strcat(filename,tempname);
		
		if(RAM_f_open(4,filename))
			return 0;	

		width[index] = ptr.img_xlen;
		height[index] = ptr.img_ylen;
	
		index++;
	}

	sum_width = 0;
	sum_height = 0;
	
	
	if(dispbuf[0]=='0')
	{
		for(i=1;i<5;i++)
		{
			sum_width += width[i];
			sum_height += height[i];
		}
	}
	else
	{
		for(i=0;i<5;i++)
		{
			sum_width += width[i];
			sum_height += height[i];
		}
	}
	
	//sp.x = (SYS_TIME.img_xlen - sum_width)/2;
	sp.x = 0;
	sp.y = (SYS_TIME.img_ylen - sum_height)/2;

	if((G_DispMode == dmode_180C) || (G_DispMode == dmode_270C))
	{
		//sp.x = (DisplayRegion[2].x_len - sum_width)/2 + sum_width;
		sp.x = DisplayRegion[2].x_len;
		sp.y = (DisplayRegion[2].y_len - sum_height)/2 + sum_height;
	}

	if(dispbuf[0]=='0')
		i = 1;
	else	
		i = 0;
	while(i < 5)
	{
		
		
		if((width[i] != 0) && (height[i] != 0))
		{			
			sprintf(filename,"%s","");
			sprintf(tempname,"0x%x.bmp",dispbuf[i]);
			strcat(filename,tempname);
			if(RAM_f_open(4,filename))
				return 0;
								
			if((G_DispMode == dmode_0C) || (G_DispMode == dmode_180C))
			{
				sp.y = (DisplayRegion[2].y_len - height[i])/2;
			}
			else
			{
				//sp.x = (DisplayRegion[2].x_len - width[i])/2;
				sp.x = 0;
			}

			if(G_DispMode == dmode_180C)
			{
				sp.x -= width[i];
			}
			else if(G_DispMode == dmode_270C)
			{
				sp.y -= height[i];
			}
			if(blink_flag[i]==1)
			{
				ptr.img_base = bkgnd_fbcopy.img_base;
				v4a_drawrect(&ptr,gui_rect(0,0,ptr.img_xlen,ptr.img_ylen),ARGB(0,0,0,0));
			}
			v4a_copyimage(&ptr,&SYS_TIME,gui_point(sp.x, sp.y));

			if(G_DispMode == dmode_0C)
			{
				sp.x += width[i];
			}
			else if(G_DispMode == dmode_90C)
			{
				sp.y += height[i];
			}

		}

		i++;
	}

	
	v4a_copyimage(&SYS_TIME,&overlay_fbdisplay,gui_point(DisplayRegion[2].s_x, DisplayRegion[2].s_y));

	return 0;
}



/*******************************************************
 * 显示日期 3
*******************************************************/
unsigned char DisplayDay(void)
{
	unsigned char dispbuf[10];
	unsigned char blink_flag[10];
	unsigned int temp;
	unsigned int index;
	char buf[100];
	GUIRECT rect;
	GUIPOINT sp;
	BMPINFO *info;
	BMPINFO infotemp;
	unsigned int i;
	char filename[64];
	char tempname[16];
	unsigned char flag_error;
	unsigned int width[10];
	unsigned int height[10];
	unsigned int sum_width,sum_height;

	unsigned char * fileptr;
	unsigned char index_file;

	time_t timer =time(NULL);
	sys_time = localtime(&timer);

	info = &infotemp;

	if((DisplayRegion[3].x_len == 0) || (DisplayRegion[3].y_len == 0))
	{
		return 0;
	}

//如果日期没有变化，则不更新显示
	//if(date_config_flag==0)
	//if((sys_time->tm_year == Dt_last_year) && (sys_time->tm_mon == Dt_last_month) && (sys_time->tm_mday == Dt_last_day))
	//{
	//	return 0;
	//}

	if((sys_time->tm_mon < 0) || (sys_time->tm_mon > 11))
	{
		sys_time->tm_mon = 0;
	}
	if((sys_time->tm_mday < 1) || (sys_time->tm_mday > 31))
	{
		sys_time->tm_mday = 1;
	}
	
	for(index=0;index<10;index++)
	{
		dispbuf[index] = 0;
		blink_flag[index] = 0;
		width[index] = 0;
		height[index] = 0;
	}

	dispbuf[0] = '2';
	dispbuf[1] = '0';
	temp = sys_time->tm_year % 100;
	dispbuf[2] = '0' + (temp / 10);
	temp = sys_time->tm_year % 10;
	dispbuf[3] = '0' + temp;
	dispbuf[4] = '.';

	//月
	temp = (sys_time->tm_mon+1) / 10;
	dispbuf[5] = '0' + temp;
	temp = (sys_time->tm_mon+1) % 10;
	dispbuf[6] = '0' + temp;
	dispbuf[7] = '.';

	//日
	temp = sys_time->tm_mday / 10;
	dispbuf[8] = '0' + temp;
	temp = sys_time->tm_mday % 10;
	dispbuf[9] = '0' + temp;

	//printf("date_config_en=%d\n",date_config_en);
	if(date_config_en==1)
	{
		
		dispbuf[date_offset]=dispbuf[date_offset]+date_change;
		if( dispbuf[date_offset] > '9')
			dispbuf[date_offset]='0';
		else if( dispbuf[date_offset] < '0')
			dispbuf[date_offset]='9';
		
		if((dispbuf[2]-'0')*10+dispbuf[3]-'0'>=36)
		{
			dispbuf[2]='3';
			dispbuf[3]='0';
		}

		if((dispbuf[5]-'0')*10+dispbuf[6]-'0'>=13)
		{
			dispbuf[5]='1';
			dispbuf[6]='0';
		}

		if((dispbuf[8]-'0')*10+dispbuf[9]-'0'>=32)
		{
			dispbuf[8]='3';
			dispbuf[9]='0';
		}

		if((dispbuf[5]-'0')*10+dispbuf[6]-'0'==0)
		{
			dispbuf[5]='0';
			dispbuf[6]='1';
		}

		if((dispbuf[8]-'0')*10+dispbuf[9]-'0'==0)
		{
			dispbuf[8]='0';
			dispbuf[9]='1';
		}

		if(date_change!=0)
		{
		printf("date_offset=%d\n",date_offset);
		memset(buf,0,100);
		sprintf(buf,"date 20%c%c.%c%c.%c%c-%d:%d",dispbuf[2],dispbuf[3],dispbuf[5],dispbuf[6],dispbuf[8],dispbuf[9],sys_time->tm_hour,sys_time->tm_min);
		system(buf);
		system("hwclock -w");
		date_change=0;
		}
		if(cur_bit)
			blink_flag[date_offset]=1;

	}

	SYS_DAY.img_base = 8*1024*1024;
	SYS_DAY.img_xlen = DisplayRegion[3].x_len;
	SYS_DAY.img_ylen = DisplayRegion[3].y_len;
	SYS_DAY.img_bpp = 32;
	v4a_drawrect(&SYS_DAY,gui_rect(0,0,DisplayRegion[3].x_len,DisplayRegion[3].y_len),ARGB(0,0,0,0));

//计算显示区域长宽
	index = 0;
	while(index < 10)
	{
		if(dispbuf[index] == 0)
		{
			break;
		}

		sprintf(filename,"%s","");
		sprintf(tempname,"0x%x.bmp",dispbuf[index]);
		strcat(filename,tempname);

		if(RAM_f_open(5,filename))
		{
			printf("RAM_f_open %s failed\n",filename);
			return 0;
		}
		width[index] = ptr.img_xlen;
		height[index] = ptr.img_ylen;
	
		index++;		
	}

	sum_width = 0;
	sum_height = 0;
	for(i=2;i<10;i++)
	{
		sum_width += width[i];
		sum_height += height[i];
	}
	
	sp.x = DisplayRegion[3].x_len > sum_width ? (DisplayRegion[3].x_len - sum_width)/2 : 0;
	sp.y = DisplayRegion[3].y_len > sum_height ? (DisplayRegion[3].y_len - sum_height)/2 : 0;

	if((G_DispMode == dmode_180C) || (G_DispMode == dmode_270C))
	{
		sp.x = DisplayRegion[3].x_len > sum_width ? (DisplayRegion[3].x_len - sum_width)/2 + sum_width : DisplayRegion[3].x_len;
		sp.y = DisplayRegion[3].y_len > sum_height ? (DisplayRegion[3].y_len - sum_height)/2 + sum_height : DisplayRegion[3].y_len;
	}
	i = 2;
	while(i < 10)
	{
		if((width[i] != 0) && (height[i] != 0))
		{
			sprintf(filename,"%s","");
			sprintf(tempname,"0x%x.bmp",dispbuf[i]);
			strcat(filename,tempname);

			if(RAM_f_open(5,filename))
				return 0;

			if((G_DispMode == dmode_0C) || (G_DispMode == dmode_180C))
			{
					
				sp.y = (DisplayRegion[3].y_len - height[i])/2;
			}
			else
			{
				sp.x = (DisplayRegion[3].x_len - width[i])/2;
			}

			if(G_DispMode == dmode_180C)
			{
				sp.x -= width[i];
			}
			else if(G_DispMode == dmode_270C)
			{
				sp.y -= height[i];
			}

			if(blink_flag[i]==1)
			{
				ptr.img_base = bkgnd_fbcopy.img_base;
				v4a_drawrect(&ptr,gui_rect(0,0,ptr.img_xlen,ptr.img_ylen),ARGB(0,0,0,0));				
			}
			v4a_copyimage(&ptr,&SYS_DAY,gui_point(sp.x, sp.y));
			
			if(G_DispMode == dmode_0C)
			{
				sp.x += width[i];
			}
			else if(G_DispMode == dmode_90C)
			{
				sp.y += height[i];
			}
		}

		i++;
	}

	v4a_copyimage(&SYS_DAY,&overlay_fbdisplay,gui_point(DisplayRegion[3].s_x, DisplayRegion[3].s_y));

	Dt_last_year=sys_time->tm_year;
	Dt_last_month=sys_time->tm_mon;
	Dt_last_day=sys_time->tm_mday;



	return 0;
}



/*******************************************************
 * 显示星期 4
*******************************************************/
unsigned char DisplayWeek(void)
{
	GUIRECT rect;
	GUIPOINT dp;
	BMPINFO *info;
	BMPINFO infotemp;
	char filename[64];

	unsigned char * fileptr;
	unsigned char index;
	int Dt_weekday;
	
	time_t timer =time(NULL);
	sys_time = localtime(&timer);

	info = &infotemp;

	if((DisplayRegion[4].x_len == 0) || (DisplayRegion[4].y_len == 0))
	{
		return 0;
	}
	Dt_weekday = sys_time->tm_wday;
	//如果没有变化，则不更新显示
	if(Dt_last_weekday == Dt_weekday)
	{
		return 0;
	}

	SYS_WEEK.img_base = 8*1024*1024;
	SYS_WEEK.img_xlen = DisplayRegion[4].x_len;
	SYS_WEEK.img_ylen = DisplayRegion[4].y_len;
	SYS_WEEK.img_bpp = 32;
	v4a_drawrect(&SYS_WEEK,gui_rect(0,0,DisplayRegion[4].x_len,DisplayRegion[4].y_len),ARGB(0,0,0,0));

	sprintf(filename,"%s","");
	strcat(filename,weekday[Dt_weekday]);

	if(RAM_f_open(4,filename))
		return 0;
	
	if((ptr.img_xlen > DisplayRegion[4].x_len) || (ptr.img_ylen > DisplayRegion[4].y_len))
	{
		return 0;
	}

	if((ptr.img_xlen == 0) || (ptr.img_ylen == 0))
	{
		return 0;
	}

	dp.x = (DisplayRegion[4].x_len - ptr.img_xlen)/2;
	dp.y = (DisplayRegion[4].y_len - ptr.img_ylen)/2;
	
	if((G_DispMode == dmode_180C) || (G_DispMode == dmode_270C))
	{
		dp.x = (DisplayRegion[4].x_len + ptr.img_xlen)/2;
		dp.y = (DisplayRegion[4].y_len + ptr.img_ylen)/2;
	}

	if((G_DispMode == dmode_0C) || (G_DispMode == dmode_180C))
	{
		dp.y = (DisplayRegion[4].y_len - ptr.img_ylen)/2;
	}
	else
	{
		dp.x = (DisplayRegion[4].x_len - ptr.img_xlen)/2;
	}
		
	if(G_DispMode == dmode_180C)
	{
		dp.x -= ptr.img_xlen;
	}
	else if(G_DispMode == dmode_270C)
	{
		dp.y -= ptr.img_ylen;
	}
	
	v4a_copyimage(&ptr,&SYS_WEEK,gui_point(dp.x, dp.y));
	
	if(G_DispMode == dmode_0C)
	{
		dp.x += ptr.img_xlen;
	}
	else if(G_DispMode == dmode_90C)
	{
		dp.y += ptr.img_ylen;
	}
	
	v4a_copyimage(&SYS_WEEK,&overlay_fbdisplay,gui_point(DisplayRegion[4].s_x, DisplayRegion[4].s_y));
	Dt_last_weekday = Dt_weekday;
	
	return 0;
	
			
}



/*******************************************************
 * 显示电梯楼层
*******************************************************/
int floormap_init()
{
	char buf[40];
	char *s;
	FILE* p = fopen("/v4asys/LIFT/flrmap.csv","rb");
	if(p == NULL)
		return 0;
	int i=0;
	char tmp[3];
	flr_map_flag = 1;
	memset(buf,0,40);
	
	while(fgets(buf,40,p)!=NULL)
	{	
		if((s=strstr(buf,",")) != NULL)
		{
			memset(&flr_map[i],0,8);
			
			if( (s-buf)==3 )
			{			
				memcpy(flr_map[i].flr0,buf,3);
				flr_map[i].flr0[3]='\0';
				
				memset(tmp,0,3);
				if( strlen(buf) ==9 )
				{	
					memcpy(flr_map[i].flr1,s+1,3);
				}
				else if( strlen(buf) ==8 )
				{	
					memcpy(tmp,s+1,2);
					sprintf(flr_map[i].flr1," %s",tmp);
				}
				else if( strlen(buf) ==7 )
				{	
					memcpy(tmp,s+1,1);
					sprintf(flr_map[i].flr1,"  %s",tmp);
				}
			}
			else if( (s-buf)==2 )
			{	
				memset(tmp,0,3);		
				memcpy(tmp,buf,2);
				sprintf(flr_map[i].flr0," %s",tmp);
				flr_map[i].flr0[3]='\0';
				
				memset(tmp,0,3);
				if( strlen(buf) ==8 )
				{	
					memcpy(flr_map[i].flr1,s+1,3);
				}
				else if( strlen(buf) ==7 )
				{	
					memcpy(tmp,s+1,2);
					sprintf(flr_map[i].flr1," %s",tmp);
				}
				else if( strlen(buf) ==6 )
				{	
					memcpy(tmp,s+1,1);
					sprintf(flr_map[i].flr1,"  %s",tmp);
				}
				flr_map[i].flr1[3]='\0';
			}
			else if( (s-buf)==1 )
			{	
				memset(tmp,0,3);		
				memcpy(tmp,buf,1);
				sprintf(flr_map[i].flr0,"  %s",tmp);
				flr_map[i].flr0[3]='\0';
				
				memset(tmp,0,3);
				if( strlen(buf) ==7 )
				{	
					memcpy(flr_map[i].flr1,s+1,3);
				}
				else if( strlen(buf) ==6 )
				{	
					memcpy(tmp,s+1,2);
					sprintf(flr_map[i].flr1," %s",tmp);
				}
				else if( strlen(buf) ==5 )
				{	
					memcpy(tmp,s+1,1);
					sprintf(flr_map[i].flr1,"  %s",tmp);
				}
				flr_map[i].flr1[3]='\0';
			}
			
			i++;
		}
		else
		{
		
			if((i+1)%1000/100==0)
				flr_map[i].flr0[0]=' ';
			else
				flr_map[i].flr0[0]=((i+1)%1000/100)+'0';
			
				
			if((i+1)%100/10==0)
				flr_map[i].flr0[1]=' ';
			else
				flr_map[i].flr0[1]=((i+1)%100/10)+'0';
							
			flr_map[i].flr0[2]=((i+1)%10)+'0';
			flr_map[i].flr0[3]='\0';	
			
			memset(tmp,0,3);
			if( strlen(buf) >=5 )
			{	
				memcpy(flr_map[i].flr1,buf,3);
			}
			else if( strlen(buf) ==4 )
			{	
				memcpy(tmp,buf,2);
				sprintf(flr_map[i].flr1," %s",tmp);
			}
			else if( strlen(buf) ==3 )
			{	
				memcpy(tmp,buf,1);
				sprintf(flr_map[i].flr1,"  %s",tmp);
			}	
			flr_map[i].flr1[3]='\0';
			i++;
		}
		memset(buf,0,40);
		
		//printf("flr_map[%d].flr0%s\n",i-1,flr_map[i-1].flr0);
		//printf("flr_map[%d].flr1%s\n",i-1,flr_map[i-1].flr1);
		
		if(i>=255)
			break;
		
	}
	fclose(p);
	return 0;
}

unsigned char DisplayFloor(flr flr_n)
{
	GUIPOINT sp;
	unsigned int i;
	char filename[64];
	char tempname[16];
	unsigned char flag_error;
	unsigned int width[8];
	unsigned int height[8];
	unsigned int sum_width,sum_height;

	char dispbuf[4];
	int temp;

	if((DisplayRegion[1].x_len == 0) || (DisplayRegion[1].y_len == 0))
	{
		return 0;
	}
	dispbuf[0] = ' ';
	dispbuf[1] = ' ';
	dispbuf[2] = ' ';
	dispbuf[3] = '\0';
	
	SYS_FLR.img_base = 8*1024*1024;
	SYS_FLR.img_xlen = DisplayRegion[1].x_len;
	SYS_FLR.img_ylen = DisplayRegion[1].y_len;
	SYS_FLR.img_bpp = 32;
	
	if( (flr_n.flr1 == ' ')&&(flr_n.flr2 == ' ')&&(flr_n.flr3 == ' ') )
	{
		v4a_drawrect(&SYS_FLR,gui_rect(0,0,DisplayRegion[1].x_len,DisplayRegion[1].y_len),ARGB(0,0,0,0));
		v4a_copyimage(&SYS_FLR,&overlay_fbdisplay,gui_point(DisplayRegion[1].s_x, DisplayRegion[1].s_y));
		return 0;
	}	
	else
	{	
		dispbuf[0] = flr_n.flr1;
		dispbuf[1] = flr_n.flr2;
		dispbuf[2] = flr_n.flr3;
	}
	

	v4a_drawrect(&SYS_FLR,gui_rect(0,0,DisplayRegion[1].x_len,DisplayRegion[1].y_len),ARGB(0,0,0,0));

	if(flr_map_flag == 1)
	{
		for(i=0;i<255;i++)
		{
			//printf("\n%s,%s\n",flr_map[i].flr0,flr_map[i].flr1);
			if(!strcmp(dispbuf,flr_map[i].flr0))
			{	
				memcpy(dispbuf,flr_map[i].flr1,3);
				break;
			}
			continue;
		}
	}

	//计算显示区域长宽
	for(i=0;i<3;i++)
	{
		
		sprintf(filename,"%s","");
		sprintf(tempname,"0x%x.bmp",dispbuf[i]);
		strcat(filename,tempname);
		if(RAM_f_open(1,filename))
		{
			width[i] = 0;
			height[i] = 0;
		}
		else
		{
			width[i] = ptr.img_xlen;
			height[i] = ptr.img_ylen;
		}
	}

		sum_width = 0;
		sum_height = 0;
		for(i=0;i<3;i++)
		{
			sum_width += width[i];
			sum_height += height[i];
		}

		sp.x = (DisplayRegion[1].x_len - sum_width)/2;
		sp.y = (DisplayRegion[1].y_len - sum_height)/2;

		if((G_DispMode == dmode_180C) || (G_DispMode == dmode_270C))
		{
			sp.x = (DisplayRegion[1].x_len - sum_width)/2 + sum_width;
			sp.y = (DisplayRegion[1].y_len - sum_height)/2 + sum_height;
		}

		for(i=0;i<3;i++)
		{
			sprintf(filename,"%s","");
			sprintf(tempname,"0x%x.bmp",dispbuf[i]);
			strcat(filename,tempname);
			
			if(RAM_f_open(1,filename))
				continue;
		
			if((G_DispMode == dmode_0C) || (G_DispMode == dmode_180C))
			{
				sp.y = (DisplayRegion[1].y_len - height[i])/2;
			}
			else
			{
				sp.x = (DisplayRegion[1].x_len - width[i])/2;
			}

			if(G_DispMode == dmode_180C)
			{
				sp.x -= width[i];
			}
			else if(G_DispMode == dmode_270C)
			{
				sp.y -= height[i];
			}

			v4a_copyimage(&ptr,&SYS_FLR,gui_point(sp.x, sp.y));
			if(G_DispMode == dmode_0C)
			{
				sp.x += width[i];
			}
			else if(G_DispMode == dmode_90C)
			{
				sp.y += height[i];
			}
		}
		

	v4a_copyimage(&SYS_FLR,&overlay_fbdisplay,gui_point(DisplayRegion[1].s_x, DisplayRegion[1].s_y));

	return 0;
}


/*******************************************************
 * 显示电梯运行方向箭头
*******************************************************/
unsigned char DisplayDir(char Dir_num)
{
	GUIPOINT dp;
	char filename[64];
	GUIRECT rect;
	BMPINFO *info;
	BMPINFO infotemp;

	unsigned char * fileptr;
	unsigned char index;
	static int i=0;
	char Direction;
	static char Direction_old='0';
	
	Direction = Dir_num;
	
	info = &infotemp;
	
	//if(Direction_old==Direction)
	if(0)
	{
		return 0;
	}

	if((DisplayRegion[0].x_len == 0) || (DisplayRegion[0].y_len == 0))
	{
		return 0;
	}

	if(!((Direction <= '4') && (Direction >= '0')))
	{
		return 0;
	}
	
	Direction_old=Direction;
	
	SYS_DIR.img_base = 8*1024*1024;
	SYS_DIR.img_xlen = DisplayRegion[0].x_len;
	SYS_DIR.img_ylen = DisplayRegion[0].y_len;
	SYS_DIR.img_bpp = 32;

	v4a_drawrect(&SYS_DIR,gui_rect(0,0,DisplayRegion[0].x_len,DisplayRegion[0].y_len),ARGB(0,0,0,0));
	
	switch(Direction)
	{
		case '0':
			i=0;
			v4a_copyimage(&SYS_DIR,&overlay_fbdisplay,gui_point(DisplayRegion[0].s_x, DisplayRegion[0].s_y));
			return 0;
		break;

		case '1':
			sprintf(filename,"%s","");
			strcat(filename,DirFile_U[0]);
		break;

		case '2':
			sprintf(filename,"%s","");
			strcat(filename,DirFile_U[i%6]);
		break;

		case '3':
			sprintf(filename,"%s","");
			strcat(filename,DirFile_D[0]);
		break;

		case '4':
			sprintf(filename,"%s","");
			strcat(filename,DirFile_D[i%6]);
		break;

		default:
			return 0;
		break;
	}
	
	i++;

	if(RAM_f_open(2,filename))
		return 0;
	dp.x = (DisplayRegion[0].x_len - ptr.img_xlen)/2;
	dp.y = (DisplayRegion[0].y_len - ptr.img_ylen)/2;

	if((G_DispMode == dmode_180C) || (G_DispMode == dmode_270C))
	{
		dp.x = (DisplayRegion[0].x_len + ptr.img_xlen)/2;
		dp.y = (DisplayRegion[0].y_len + ptr.img_ylen)/2;
	}

	if((G_DispMode == dmode_0C) || (G_DispMode == dmode_180C))
	{
		dp.y = (DisplayRegion[0].y_len - ptr.img_ylen)/2;
	}
	else
	{
		dp.x = (DisplayRegion[0].x_len - ptr.img_xlen)/2;
	}

	if(G_DispMode == dmode_180C)
	{
		dp.x -= ptr.img_xlen;
	}
	else if(G_DispMode == dmode_270C)
	{
		dp.y -= ptr.img_ylen;
	}

	v4a_copyimage(&ptr,&SYS_DIR,gui_point(dp.x, dp.y));

	if(G_DispMode == dmode_0C)
	{
		dp.x += ptr.img_xlen;
	}
	else if(G_DispMode == dmode_90C)
	{
		dp.y += ptr.img_ylen;
	}

	v4a_copyimage(&SYS_DIR,&overlay_fbdisplay,gui_point(DisplayRegion[0].s_x, DisplayRegion[0].s_y));
	
	sprintf(filename,"%s","");
	strcat(filename,DirFile_U[i%6]);
	if(RAM_f_open(2,filename))
	{
		i=0;
	}
	
	return 0;
}


/*******************************************************
 * 显示电梯状态
*******************************************************/
unsigned char DisplayState(char state_num)
{
	GUIPOINT dp;
	char filename[64];
	GUIRECT rect;
	BMPINFO *info;
	BMPINFO infotemp;

	unsigned char * fileptr;
	unsigned char index;
	int i;
	char Direction;
	Direction = state_num;
	static int state_flag=0;
	
	info = &infotemp;

	if((DisplayRegion[5].x_len == 0) || (DisplayRegion[5].y_len == 0))
	{
		return 0;
	}

	if(!((Direction <= '7') && (Direction >= '0')))
	{
		v4a_drawrect(&SYS_STE,gui_rect(0,0,DisplayRegion[5].x_len,DisplayRegion[5].y_len),ARGB(0,0,0,0));
		v4a_copyimage(&SYS_STE,&overlay_fbdisplay,gui_point(DisplayRegion[5].s_x, DisplayRegion[5].s_y));
		return 0;

	}

	SYS_STE.img_base = 8*1024*1024;
	SYS_STE.img_xlen = DisplayRegion[5].x_len;
	SYS_STE.img_ylen = DisplayRegion[5].y_len;
	SYS_STE.img_bpp = 32;

	v4a_drawrect(&SYS_STE,gui_rect(0,0,DisplayRegion[5].x_len,DisplayRegion[5].y_len),ARGB(0,0,0,0));

	index = Direction - '0';
		
	sprintf(filename,"%s","");
	strcat(filename,filealarm[2*index+state_flag]);
			
	if(RAM_f_open(3,filename))
		return 0;
	dp.x = (DisplayRegion[5].x_len - ptr.img_xlen)/2;
	dp.y = (DisplayRegion[5].y_len - ptr.img_ylen)/2;
	
	if((G_DispMode == dmode_180C) || (G_DispMode == dmode_270C))
	{
		dp.x = (DisplayRegion[5].x_len + ptr.img_xlen)/2;
		dp.y = (DisplayRegion[5].y_len + ptr.img_ylen)/2;
	}

	if((G_DispMode == dmode_0C) || (G_DispMode == dmode_180C))
	{
		dp.y = (DisplayRegion[5].y_len - ptr.img_ylen)/2;
	}
	else
	{
		dp.x = (DisplayRegion[5].x_len - ptr.img_xlen)/2;
	}

	if(G_DispMode == dmode_180C)
	{
		dp.x -= ptr.img_xlen;
	}
	else if(G_DispMode == dmode_270C)
	{
		dp.y -= ptr.img_ylen;
	}

	v4a_copyimage(&ptr,&SYS_STE,gui_point(dp.x, dp.y));

	if(G_DispMode == dmode_0C)
	{
		dp.x += ptr.img_xlen;
	}
	else if(G_DispMode == dmode_90C)
	{
		dp.y += ptr.img_ylen;
	}
	
	v4a_copyimage(&SYS_STE,&overlay_fbdisplay,gui_point(DisplayRegion[5].s_x, DisplayRegion[5].s_y));

	state_flag=1-state_flag;
	return 0;
}


/*******************************************************
 * 显示天气
*******************************************************/
extern char weather_info0[4][4];
int DisplayWeather(void)
{
	GUIRECT rect;
	GUIPOINT dp;
	BMPINFO *info;
	BMPINFO infotemp;
	char picture_full_name[64];
	int i,j;
	static int wtim=0;
	int WeatherWidth;
	int w_flag = 0;

	/*if(wtim>=60)
		wtim=0;
	if(wtim!=0)
	{
		wtim++;
		return 0;
	}
	*/

	int weather_dpx,weather_dpy;
	int readlen = 0,index;
	char MD5Code[40];
	FILE *txt_fp;
	char *s;
	int len;
	int flen[2];
	memset(userbkgnd_bmp,0,1366*768*4);
	memset(picture_full_name,0,64);
	sprintf(picture_full_name,"/v4asys/weather/weather_%s.bmp",weather_info0[0]);

	if((DisplayRegion[7].x_len == 0) || (DisplayRegion[7].y_len == 0))
	{
		return 0;
	}

	v4a_drawrect(&overlay_fbdisplay,gui_rect(DisplayRegion[7].s_x,DisplayRegion[7].s_y,DisplayRegion[7].x_len,DisplayRegion[7].y_len),ARGB(0,0,0,0));
	
	FILE *pictu = fopen(picture_full_name,"r");
	if(pictu>0)
		;
	else
	{
		printf("open weather file failed\n");
		pictu = fopen("/v4asys/weather/weather_99.bmp","r");
		if(pictu==NULL)
		{
			printf("open weather_99.bmp file failed\n");
			return 0;
		}
		w_flag = 1;
	}

	readlen = fread(userbkgnd_bmp,4*1366*768,1,pictu);
	fclose(pictu);

	BMPINFO* userbkgnd_info = bmp_getinfo(userbkgnd_bmp);
	if(DisplayRegion[7].y_len<userbkgnd_info->height)
		return 0;
	if(DisplayRegion[7].x_len>=userbkgnd_info->width)
	{
		userbkgnd_pic.img_base = bkgnd_fbcopy.img_base;
		userbkgnd_pic.img_xlen = userbkgnd_info->width;
		userbkgnd_pic.img_ylen = userbkgnd_info->height;
		userbkgnd_pic.img_bpp = 32;

	v4a_drawrect(&userbkgnd_pic,gui_rect(0,0,userbkgnd_pic.img_xlen,userbkgnd_pic.img_ylen),ARGB(0,0,0,0));

	
		dp.x = (DisplayRegion[7].x_len - userbkgnd_pic.img_xlen)/2;
		dp.y = (DisplayRegion[7].y_len - userbkgnd_pic.img_ylen)/2;

		if((G_DispMode == dmode_180C) || (G_DispMode == dmode_270C))
		{
			dp.x = (DisplayRegion[7].x_len + userbkgnd_pic.img_xlen)/2;
			dp.y = (DisplayRegion[7].y_len + userbkgnd_pic.img_xlen)/2;
		}

		if((G_DispMode == dmode_0C) || (G_DispMode == dmode_180C))
		{
			dp.y = (DisplayRegion[7].y_len - userbkgnd_pic.img_ylen)/2;
		}
		else
		{
			dp.x = (DisplayRegion[7].x_len - userbkgnd_pic.img_xlen)/2;
		}

		if(G_DispMode == dmode_180C)
		{
			dp.x -= userbkgnd_pic.img_xlen;
		}

		else if(G_DispMode == dmode_270C)
		{
			dp.y -= userbkgnd_pic.img_ylen;
		}

	memset(userbkgnd_bmp_data,0,1366*768*4);
	
	for(i = 0;i<(int)(userbkgnd_info->height);i++)
		memcpy(&userbkgnd_bmp_data[((int)(userbkgnd_info->height)-1-i)*4*(int)(userbkgnd_info->width)],userbkgnd_info->pixel_array+i*(int)(userbkgnd_info->width)*4,(int)(userbkgnd_info->width)*4);
	v4a_fillvram(userbkgnd_pic.img_base,userbkgnd_info->width*userbkgnd_info->height,(int *)userbkgnd_bmp_data);
	v4a_copyimage(&userbkgnd_pic,&overlay_fbdisplay,gui_point(DisplayRegion[7].s_x+dp.x, DisplayRegion[7].s_y+dp.y));
	
	if(w_flag)
		return 0;
	
	char weather_txt[20];
	memset(weather_txt,0,20);
	strcat(weather_txt,weather_info0[1]);
	strcat(weather_txt,"A");
	strcat(weather_txt,weather_info0[2]);
	strcat(weather_txt,"B");
	
	if((G_DispMode == dmode_0C) || (G_DispMode == dmode_90C))
	{
	
		weather_dpx = 89;
		weather_dpy = 43;
		for(j=0;j<strlen(weather_txt);j++)
		{
			memset(picture_full_name,0,64);
		sprintf(picture_full_name,"/v4asys/weather/0x%x.bmp",weather_txt[j]);
		
		printf("weather %s\n",picture_full_name);	
		pictu = fopen(picture_full_name,"r");
		if(pictu>0)
			;
		else
		{
			printf("open weather file failed\n");
			continue;
		}
		memset(userbkgnd_bmp,0,1366*768*4);
		memset(userbkgnd_bmp_data,0,1366*768*4);
		readlen = fread(userbkgnd_bmp,4*1366*768,1,pictu);
		fclose(pictu);

		BMPINFO* userbkgnd_info1 = bmp_getinfo(userbkgnd_bmp);
		
		userbkgnd_pic0.img_base = bkgnd_fbcopy.img_base;
		userbkgnd_pic0.img_xlen = userbkgnd_info1->width;
		userbkgnd_pic0.img_ylen = userbkgnd_info1->height;
		userbkgnd_pic0.img_bpp = 32;
		
		
		v4a_drawrect(&userbkgnd_pic0,gui_rect(0,0,userbkgnd_pic0.img_xlen,userbkgnd_pic0.img_ylen),ARGB(0,0,0,0));

		
		for(i = 0;i<(int)(userbkgnd_info1->height);i++)
			memcpy(&userbkgnd_bmp_data[((int)(userbkgnd_info1->height)-1-i)*4*(int)(userbkgnd_info1->width)],userbkgnd_info1->pixel_array+i*(int)(userbkgnd_info1->width)*4,(int)(userbkgnd_info1->width)*4);	
		
		
		v4a_fillvram(userbkgnd_pic0.img_base,userbkgnd_info1->width*userbkgnd_info1->height,(int *)userbkgnd_bmp_data);	
		v4a_copyimage(&userbkgnd_pic0,&overlay_fbdisplay,gui_point(DisplayRegion[7].s_x+dp.x+weather_dpx, DisplayRegion[7].s_y+dp.y+weather_dpy));
		weather_dpx+=userbkgnd_info1->width;
		if(weather_dpx>260)	
			break;
		}
	}
	else
	{
	
		weather_dpx = 171;
		weather_dpy = 11;
		for(j=0;j<strlen(weather_txt);j++)
		{
			memset(picture_full_name,0,64);
		sprintf(picture_full_name,"/v4asys/weather/0x%x.bmp",weather_txt[j]);
		
		printf("weather %s\n",picture_full_name);	
		pictu = fopen(picture_full_name,"r");
		if(pictu>0)
			;
		else
		{
			printf("open weather file failed\n");
			continue;
		}
		memset(userbkgnd_bmp,0,1366*768*4);
		memset(userbkgnd_bmp_data,0,1366*768*4);
		readlen = fread(userbkgnd_bmp,4*1366*768,1,pictu);
		fclose(pictu);

		BMPINFO* userbkgnd_info1 = bmp_getinfo(userbkgnd_bmp);
		
		userbkgnd_pic0.img_base = bkgnd_fbcopy.img_base;
		userbkgnd_pic0.img_xlen = userbkgnd_info1->width;
		userbkgnd_pic0.img_ylen = userbkgnd_info1->height;
		userbkgnd_pic0.img_bpp = 32;
		
		v4a_drawrect(&userbkgnd_pic0,gui_rect(0,0,userbkgnd_pic0.img_xlen,userbkgnd_pic0.img_ylen),ARGB(0,0,0,0));

		
		for(i = 0;i<(int)(userbkgnd_info1->height);i++)
			memcpy(&userbkgnd_bmp_data[((int)(userbkgnd_info1->height)-1-i)*4*(int)(userbkgnd_info1->width)],userbkgnd_info1->pixel_array+i*(int)(userbkgnd_info1->width)*4,(int)(userbkgnd_info1->width)*4);	
		
		weather_dpx-=userbkgnd_info1->width;
		if(weather_dpx<=0)	
			break;
		v4a_fillvram(userbkgnd_pic0.img_base,userbkgnd_info1->width*userbkgnd_info1->height,(int *)userbkgnd_bmp_data);	
		v4a_copyimage(&userbkgnd_pic0,&overlay_fbdisplay,gui_point(DisplayRegion[7].s_x+dp.x+weather_dpx, DisplayRegion[7].s_y+dp.y+weather_dpy));
		}
	}

	}

	
	return 0;
}


/*******************************************************
 * 显示LOGO
*******************************************************/
unsigned char DisplayLogo()
{
	GUIPOINT dp;
	char filename[64];
	GUIRECT rect;
	BMPINFO *info;
	int i;
	FILE *p;

	if((DisplayRegion[10].x_len == 0) || (DisplayRegion[10].y_len == 0))
	{
		return 0;
	}

	if(access("/v4asys/logo/logo.bmp",0)==0)
	{
		p = fopen("/v4asys/logo/logo.bmp","rb");
	}

	else
		p = fopen(pDir_logo[G_DispMode],"rb");
	
	if(p>0)
		;
	else
	{
		printf("open logo.bmp failed\n");
		return 0;
	}
	
	fread(userbkgnd_bmp,4*1366*768,1,p);
	fclose(p);
	
	if( (userbkgnd_bmp[0] == 0x42)&&(userbkgnd_bmp[1] == 0x4d) )
		;
	else
		return 1;
			
			
	info = bmp_getinfo(userbkgnd_bmp);
	
	if( (DisplayRegion[10].x_len < info->width) || (DisplayRegion[10].y_len < info->height) )
	{
		return 0;
	}
	
	SYS_LOGO.img_base = 8*1024*1024;
	SYS_LOGO.img_xlen = DisplayRegion[10].x_len;
	SYS_LOGO.img_ylen = DisplayRegion[10].y_len;
	SYS_LOGO.img_bpp = 32;
	
	ptr.img_base = bkgnd_fbcopy.img_base;
	ptr.img_xlen = info->width;
	ptr.img_ylen = info->height;
	ptr.img_bpp = 32;
	
	if( (info->width>DisplayRegion[10].x_len) || (info->height>DisplayRegion[10].y_len) )
		return 0;
		
	v4a_drawrect(&SYS_LOGO,gui_rect(0,0,DisplayRegion[10].x_len,DisplayRegion[10].y_len),ARGB(0,0,0,0));
	v4a_drawrect(&ptr,gui_rect(0,0,info->width,info->height),ARGB(0,0,0,0));
	
	
	if(info->bpp==16)
  	{
  		int m=0;
  		if(info->width*2%4!=0)
			m=4-info->width*2%4;
  		for(i = 0;i<(int)(info->height);i++)					  	
		memcpy(&userbkgnd_bmp_data[((int)(info->height)-1-i)*info->bpp/8*(int)(info->width)],\
			info->pixel_array+i*((int)(info->width)*info->bpp/8+m),(int)(info->width)*info->bpp/8);	
			
		unsigned short * pu16Temp;
		unsigned char u8R, u8G, u8B;	
		
		pu16Temp = (unsigned short *) userbkgnd_bmp_data;
		memset(userbkgnd_bmp,0,4*1366*768);
		for ( i = 0; i < (int)(info->width)*(int)(info->height); i++ )
		{
			u8R = ((pu16Temp[i] & 0xF800) >> 11) << 3;
			u8G = ((pu16Temp[i] & 0x07E0) >> 5) << 2;
			u8B = (pu16Temp[i] & 0x001F) << 3;
			userbkgnd_bmp[i*4] = u8B;
			userbkgnd_bmp[i*4 + 1] = u8G;
			userbkgnd_bmp[i*4 + 2] = u8R;
			userbkgnd_bmp[i*4 + 3] = 0xFF;
		}		
		memcpy(userbkgnd_bmp_data,userbkgnd_bmp,4*1366*768);	
  	}
  	else
	{
		for(i = 0;i<(int)(info->height);i++)
			memcpy(&userbkgnd_bmp_data[((int)(info->height)-1-i)*4*(int)(info->width)],info->pixel_array+i*(int)(info->width)*4,(int)(info->width)*4);
		
	}
	v4a_fillvram(ptr.img_base,info->width*info->height,(int *)userbkgnd_bmp_data);

	dp.x = (DisplayRegion[10].x_len - info->width)/2;
	dp.y = (DisplayRegion[10].y_len - info->height)/2;
	
	if((G_DispMode == dmode_180C) || (G_DispMode == dmode_270C))
	{
		dp.x = (DisplayRegion[10].x_len + info->width)/2;
		dp.y = (DisplayRegion[10].y_len + info->height)/2;
	}

	if((G_DispMode == dmode_0C) || (G_DispMode == dmode_180C))
	{
		dp.y = (DisplayRegion[10].y_len - info->height)/2;
	}
	else
	{
		dp.x = (DisplayRegion[10].x_len - info->width)/2;
	}

	if(G_DispMode == dmode_180C)
	{
		dp.x -= info->width;
	}
	else if(G_DispMode == dmode_270C)
	{
		dp.y -= info->height;
	}

	v4a_copyimage(&ptr,&SYS_LOGO,gui_point(dp.x, dp.y));

	if(G_DispMode == dmode_0C)
	{
		dp.x += info->width;
	}
	else if(G_DispMode == dmode_90C)
	{
		dp.y += info->height;
	}
	
	v4a_copyimage(&SYS_LOGO,&overlay_fbdisplay,gui_point(DisplayRegion[10].s_x, DisplayRegion[10].s_y));
	
	return 0;
}



/*******************************************************
 * 显示文本广告
*******************************************************/
extern int Notice_Flag;
int display_char_func()
{
	
	//FILE *media_list_fd = fopen("/v4asys/src_update/native.SPT","r");
	//if(NULL == media_list_fd)
	//return 0;
	int txt_num = 0,w;
	char txt_list[40][40];
	char notice_file[40];
	static int txt_num0 = 0;
	static int ad_flag = 0;
	int len = 0;
	int flen[2];
	
	//int *userbkgnd_bmp_txt = (int*)userbkgnd_bmp;
	int *userbkgnd_bmp_txt = NULL;

	char *s;
	char dir[40];


	char chinease[1024];

	//int BackColor = 0x2f0000ff, TextColor = 0xff000000;
		int BackColor = 0x00000000, TextColor = 0xff000000;
	int i,j,x,y,location,index,index1;
        char mat[32][4];
	char mat1[32][2];
        FILE *HZK;
	FILE *ASCII;
        
	int dpx=0,dpy=0;
	int char_num;
	int char_length;

	FILE *TXT;
	FILE *txt_fp;

	DIR *pdir = NULL;
	struct dirent *ptr = NULL;
	v4a_displayflex(1);
	if((DisplayRegion[8].x_len == 0) || (DisplayRegion[8].y_len == 0))
	{
		return 0;
	}

	/*pdir = opendir("/v4asys/VIDEO/");
	if(pdir==NULL)
	{
		//printf("opendir /v4asys/LIFT/TXT failed\n");
		return 1;
	}
	while((ptr=readdir(pdir))!=NULL)
	{
		if(strstr(ptr->d_name, ".txt"))
		{
			if(1==txt_is_valid(ptr->d_name))
			{
			memset(txt_list[txt_num], 0, 40);
			strcpy(txt_list[txt_num], ptr->d_name);			
			txt_num++;
			if(txt_num>=5)
				break;
			}
		}
	}
	closedir(pdir);
	*/
	
	for(i=0;i<255;i++)
	{
		memset(notice_file,0,40);
		sprintf(notice_file,"notice%d.txt",i);
		if(1==txt_is_valid(notice_file))
		{
			memset(txt_list[txt_num], 0, 40);
			strcpy(txt_list[txt_num], notice_file);			
			txt_num++;
			if(txt_num>=5)
				break;
		}
		
	}
	
	
	if(txt_num==0)
	{
	
		if( ( TXT = fopen("/v4asys/LIFT/ad.txt","rb") )==NULL )
			return 0;
		else 
		{
			
			fclose(TXT);
			v4a_displayflex(1);
			if(ad_flag == 1)
				return 0;
			else
			{
				ad_flag = 1;
				goto TEXT;
			}
				
		}
			
	}
	else
	{
		ad_flag = 0;
		if( (txt_num==txt_num0)&&(0==Notice_Flag) )
		return 0;
	}
		
	
	//if(txt_num==txt_num0)
		//return 0;
TEXT:
	if((HZK=fopen("/v4asys/LIFT/FONT/GB2132_32_32.FON","rb"))==NULL)
	{
		printf("no /v4asys/LIFT/FONT/GB2132_32_32.FON file\n");
        	return 0;
	}

	if((ASCII=fopen("/v4asys/LIFT/FONT/ASCII_16_32.FON","rb"))==NULL)
        {
		printf("no /v4asys/LIFT/FONT/ASCII_16_32.FON file\n");
        	return 0;
	}
	TextColor = 0xff000000+G_LFTTXT_txt_color;
	memset(userbkgnd_bmp,0,1366*768*4);
	txt_num0=txt_num;
	Notice_Flag = 0;

	GUIRECT vrect_flex;

if((G_DispMode==dmode_180C)||(G_DispMode==dmode_0C))
{	//char_num = DisplayRegion[8].x_len/16;
	userbkgnd_bmp_txt = (int *)(&userbkgnd_bmp[DisplayRegion[8].x_len*4]);
	char_length = 0;
	for(w=0;w<txt_num;w++)
	{
		memset(dir, 0, 40);
		strcpy(dir,"/v4asys/VIDEO/");
		strcat(dir,txt_list[w]);
		memset(chinease, 0, 1024);

		if((TXT=fopen(dir,"r"))==NULL)
		{
			printf("open:%s failed\n",dir);
        		continue;
		}
		fread(chinease,1,1024,TXT);
		for(index=0;index<strlen(chinease);index++)
		{
			if(chinease[index]&0x80)
			{
				index++;
				char_length+=32;
			}
			else
			{	
				if( (chinease[index]==0x0d)	|| (chinease[index]==0x0a) )
						;
				else
				char_length+=16;
			}
			
		}
		fclose(TXT);
		if( w!=(txt_num-1) )
			char_length+=160;
	}
	
	//1280*1024/32*16=2560 最大字符宽度

	char_length = char_length + DisplayRegion[8].x_len+160;
	
	printf("char_length0 = %d\n",char_length);
	
	if(char_length > 2010*16)
		char_length = 2010*16;
	printf("char_length1 = %d\n",char_length);
	for(w=0;w<txt_num;w++)
	{
		memset(dir, 0, 40);
		strcpy(dir,"/v4asys/VIDEO/");
		strcat(dir,txt_list[w]);
		memset(chinease, 0, 1024);

		if((TXT=fopen(dir,"r"))==NULL)
		{
			printf("open:%s failed\n",dir);
        		continue;
		}
		fread(chinease,1,1024,TXT);

		for(index=0,index1=0;index<strlen(chinease);index++,index1++)
		{
			
			if(dpx>2010*16)
				break;
			memset(mat,0,32*4);
			memset(mat1,0,32*2);
			if(chinease[index]&0x80)
			{
				i=chinease[index]-160;
				index++;
				index1++;
				j=chinease[index]-160;
		    		fseek(HZK,(94*(i-1)+(j-1))*32*4l,SEEK_SET);
		    		fread(mat,1,32*4,HZK);			

				for(y=0;y<32;y++)
				for(i=0;i<4;i++)
				for(x=0;x<8;x++)
				{
				    location = (8*i+x+dpx) + (y+dpy) * char_length;
				    if((mat[y][i] & (1 << (7-x))) != 0x00)
				    {
				    	userbkgnd_bmp_txt[location] = TextColor;
				    }
				    else
					userbkgnd_bmp_txt[location] = BackColor;
				}
				
				dpx += 32;
			}

			else
			{
				if( (chinease[index]==0x0d)	|| (chinease[index]==0x0a) )
						continue;
				
				fseek(ASCII,chinease[index]*32*2l,SEEK_SET);
				fread(mat1,1,32*2,ASCII);
				
			
				for(y=0;y<32;y++)
				for(i=0;i<2;i++)
				for(x=0;x<8;x++)
				{
				    location = (8*i+x+dpx) + (y+dpy) * char_length;
				    if((mat1[y][i] & (1 << (7-x))) != 0x00)
				    {
				    	userbkgnd_bmp_txt[location] = TextColor;
				    }
				    else
					userbkgnd_bmp_txt[location] = BackColor;
				}			

				dpx += 16;
			}
	
		}
       
		fclose(TXT);
		if(dpx>2010*16)
			break;
		if( w!=(txt_num-1) )
			dpx+=160;

	}
	printf("char_length2 = %d\n",dpx);
	if(ad_flag==1)
	{
		char_length = 0;
		memset(chinease, 0, 1024);

		if((TXT=fopen("/v4asys/LIFT/ad.txt","r"))==NULL)
		{
			printf("open:%s failed\n",dir);
        		return 0;
		}
		fread(chinease,1,1024,TXT);

		for(index=0;index<strlen(chinease);index++)
		{
			if(chinease[index]&0x80)
			{
				index++;
				char_length+=32;
			}
			else
			{	
				if( (chinease[index]==0x0d)	|| (chinease[index]==0x0a) )
						;
				else			
				char_length+=16;
			}
			
		}
		
		char_length += DisplayRegion[8].x_len+160;
		
		if(char_length > 2010*16)
			char_length = 2010*16;

		for(index=0,index1=0;index<strlen(chinease);index++,index1++)
		{
			if(dpx>2010*16)
				break;
			memset(mat,0,32*4);
			memset(mat1,0,32*2);
			if(chinease[index]&0x80)
			{
				i=chinease[index]-160;
				index++;
				index1++;
				j=chinease[index]-160;
		    		fseek(HZK,(94*(i-1)+(j-1))*32*4l,SEEK_SET);
		    		fread(mat,1,32*4,HZK);

						

				for(y=0;y<32;y++)
				for(i=0;i<4;i++)
				for(x=0;x<8;x++)
				{
				    location = (8*i+x+dpx) + (y+dpy) * char_length;
				    if((mat[y][i] & (1 << (7-x))) != 0x00)
				    {
				    	userbkgnd_bmp_txt[location] = TextColor;
				    }
				    else
					userbkgnd_bmp_txt[location] = BackColor;
				}
				dpx += 32;	
			}

			else
			{
				if( (chinease[index]==0x0d)	|| (chinease[index]==0x0a) )
						continue;
				
				fseek(ASCII,chinease[index]*32*2l,SEEK_SET);
				fread(mat1,1,32*2,ASCII);
				
			
				for(y=0;y<32;y++)
				for(i=0;i<2;i++)
				for(x=0;x<8;x++)
				{
				    location = (8*i+x+dpx) + (y+dpy) * char_length;
				    if((mat1[y][i] & (1 << (7-x))) != 0x00)
				    {
				    	userbkgnd_bmp_txt[location] = TextColor;
				    }
				    else
					userbkgnd_bmp_txt[location] = BackColor;
				}			

				dpx += 16;
			}
	
		}
		fclose(TXT);
	}

	vrect_flex.width = char_length;
	vrect_flex.height = 32;
	
	vrect_flex.lvertex.x = 0;
	vrect_flex.lvertex.y = 0;
	v4a_displayflex(0);
	v4a_setflexarea(&vrect_flex,gui_rect(DisplayRegion[8].s_x,DisplayRegion[8].s_y,DisplayRegion[8].x_len,DisplayRegion[8].y_len));
	v4a_drawrect(&flex_fbdisplay,gui_rect(0,0,vrect_flex.width,vrect_flex.height),ARGB(0,0,0,0));

}
else if((G_DispMode==dmode_90C)||(G_DispMode==dmode_270C))
{	
	char_num = DisplayRegion[8].y_len/16;
	for(w=0;w<txt_num;w++)
	{
		memset(dir, 0, 40);
		strcpy(dir,"/v4asys/VIDEO/");
		strcat(dir,txt_list[w]);
		memset(chinease, 0, 1024);

		if((TXT=fopen(dir,"r"))==NULL)
		{
			printf("open:%s failed\n",dir);
        		continue;
		}
		fread(chinease,1,1024,TXT);

		for(index=0,index1=0;index<strlen(chinease);index++,index1++)
		{
			memset(mat,0,32*4);
			memset(mat1,0,32*2);
			if(chinease[index]&0x80)
			{
				i=chinease[index]-160;
				index++;
				index1++;
				j=chinease[index]-160;
		    		fseek(HZK,(94*(i-1)+(j-1))*32*4l,SEEK_SET);
		    		fread(mat,1,32*4,HZK);
			

				if(( (((index1-1)%char_num)==(char_num-1)) || (((index1-1)%char_num)==0) )&& (index1 >= char_num))
				{
					dpx =dpx + 32;
					if( ((index1-1)%char_num)==(char_num-1) )
					index1++;
				}

				dpy = ((index1-1)%char_num)*16;			
			}

			else
			{
				fseek(ASCII,chinease[index]*32*2l,SEEK_SET);
				fread(mat1,1,32*2,ASCII);
				dpy = (index1%char_num)*16;

				if( ((index1%char_num)==0) && (index1 >= char_num))
				{
					dpx =dpx + 32;
				}		

			}
	
		}
		dpy = 0;
		dpx = dpx +64;         
		fclose(TXT);

	}
	
	if(ad_flag==1)
	{
		memset(chinease, 0, 1024);

		if((TXT=fopen("/v4asys/LIFT/ad.txt","r"))==NULL)
		{
			printf("open:%s failed\n",dir);
        		return 0;
		}
		fread(chinease,1,1024,TXT);

		for(index=0,index1=0;index<strlen(chinease);index++,index1++)
		{
			memset(mat,0,32*4);
			memset(mat1,0,32*2);
			if(chinease[index]&0x80)
			{
				i=chinease[index]-160;
				index++;
				index1++;
				j=chinease[index]-160;
		    		fseek(HZK,(94*(i-1)+(j-1))*32*4l,SEEK_SET);
		    		fread(mat,1,32*4,HZK);
			

				if(( (((index1-1)%char_num)==(char_num-1)) || (((index1-1)%char_num)==0) )&& (index1 >= char_num))
				{
					dpx =dpx + 32;
					if( ((index1-1)%char_num)==(char_num-1) )
					index1++;
				}

				dpy = ((index1-1)%char_num)*16;			
			}

			else
			{
				fseek(ASCII,chinease[index]*32*2l,SEEK_SET);
				fread(mat1,1,32*2,ASCII);
				dpy = (index1%char_num)*16;

				if( ((index1%char_num)==0) && (index1 >= char_num))
				{
					dpx =dpx + 32;
				}		

			}
	
		}
		fclose(TXT);
	}
	
	
	int v_txt_xlen = dpx;
	dpx = v_txt_xlen-32;
	dpy = 0;

	for(w=0;w<txt_num;w++)
	{
		memset(dir, 0, 40);
		strcpy(dir,"/v4asys/VIDEO/");
		strcat(dir,txt_list[w]);
		memset(chinease, 0, 1024);

		if((TXT=fopen(dir,"r"))==NULL)
		{
			printf("open:%s failed\n",dir);
        		continue;
		}
		fread(chinease,1,1024,TXT);

		for(index=0,index1=0;index<strlen(chinease);index++,index1++)
		{
			memset(mat,0,32*4);
			memset(mat1,0,32*2);
			if(chinease[index]&0x80)
			{
				i=chinease[index]-160;
				index++;
				index1++;
				j=chinease[index]-160;
		    		fseek(HZK,(94*(i-1)+(j-1))*32*4l,SEEK_SET);
		    		fread(mat,1,32*4,HZK);
			

				if(( (((index1-1)%char_num)==(char_num-1)) || (((index1-1)%char_num)==0) )&& (index1 >= char_num))
				{
					dpx =dpx - 32;
					if( ((index1-1)%char_num)==(char_num-1) )
					index1++;
				}

				dpy = ((index1-1)%char_num)*16;			

				for(y=0;y<32;y++)
				for(i=0;i<4;i++)
				for(x=0;x<8;x++)
				{
				    //location = (8*i+x+dpx) + (y+dpy) * DisplayRegion[8].x_len;
				    location = (dpx+31-y) + (8*i+x+dpy) * v_txt_xlen;
				    if((mat[y][i] & (1 << (7-x))) != 0x00)
				    {
				    	userbkgnd_bmp_txt[location] = TextColor;
				    }
				    else
					userbkgnd_bmp_txt[location] = BackColor;
				}
			}

			else
			{
				fseek(ASCII,chinease[index]*32*2l,SEEK_SET);
				fread(mat1,1,32*2,ASCII);
				dpx = (index1%char_num)*16;

				if( ((index1%char_num)==0) && (index1 >= char_num))
				{
					dpy =dpy - 32;
				}

			
				for(y=0;y<32;y++)
				for(i=0;i<2;i++)
				for(x=0;x<8;x++)
				{
				    location = (dpx+31-y) + (8*i+x+dpy) * v_txt_xlen;
				    if((mat1[y][i] & (1 << (7-x))) != 0x00)
				    {
				    	userbkgnd_bmp_txt[location] = TextColor;
				    }
				    else
					userbkgnd_bmp_txt[location] = BackColor;
				}			
			}
	
		}
		dpy = 0;
		dpx = dpx -64;
		fclose(TXT);
	}

	if(ad_flag==1)
	{
		memset(chinease, 0, 1024);

		if((TXT=fopen("/v4asys/LIFT/ad.txt","r"))==NULL)
		{
			printf("open:%s failed\n",dir);
        		return 0;
		}
		fread(chinease,1,1024,TXT);

		for(index=0,index1=0;index<strlen(chinease);index++,index1++)
		{
			memset(mat,0,32*4);
			memset(mat1,0,32*2);
			if(chinease[index]&0x80)
			{
				i=chinease[index]-160;
				index++;
				index1++;
				j=chinease[index]-160;
		    		fseek(HZK,(94*(i-1)+(j-1))*32*4l,SEEK_SET);
		    		fread(mat,1,32*4,HZK);
			

				if(( (((index1-1)%char_num)==(char_num-1)) || (((index1-1)%char_num)==0) )&& (index1 >= char_num))
				{
					dpx =dpx - 32;
					if( ((index1-1)%char_num)==(char_num-1) )
					index1++;
				}

				dpy = ((index1-1)%char_num)*16;			

				for(y=0;y<32;y++)
				for(i=0;i<4;i++)
				for(x=0;x<8;x++)
				{
				    //location = (8*i+x+dpx) + (y+dpy) * DisplayRegion[8].x_len;
				    location = (dpx+31-y) + (8*i+x+dpy) * v_txt_xlen;
				    if((mat[y][i] & (1 << (7-x))) != 0x00)
				    {
				    	userbkgnd_bmp_txt[location] = TextColor;
				    }
				    else
					userbkgnd_bmp_txt[location] = BackColor;
				}
			}

			else
			{
				fseek(ASCII,chinease[index]*32*2l,SEEK_SET);
				fread(mat1,1,32*2,ASCII);
				dpx = (index1%char_num)*16;

				if( ((index1%char_num)==0) && (index1 >= char_num))
				{
					dpy =dpy - 32;
				}

			
				for(y=0;y<32;y++)
				for(i=0;i<2;i++)
				for(x=0;x<8;x++)
				{
				    location = (dpx+31-y) + (8*i+x+dpy) * v_txt_xlen;
				    if((mat1[y][i] & (1 << (7-x))) != 0x00)
				    {
				    	userbkgnd_bmp_txt[location] = TextColor;
				    }
				    else
					userbkgnd_bmp_txt[location] = BackColor;
				}			
			}
	
		}
		fclose(TXT);
	}

	vrect_flex.width = DisplayRegion[8].x_len;
	vrect_flex.height = DisplayRegion[8].y_len;
	if(v_txt_xlen>DisplayRegion[8].x_len)
	vrect_flex.width = v_txt_xlen;
	vrect_flex.lvertex.x = 0;
	vrect_flex.lvertex.y = 0;
	v4a_displayflex(0);
	v4a_setflexarea(&vrect_flex,gui_rect(DisplayRegion[8].s_x,DisplayRegion[8].s_y,DisplayRegion[8].x_len,DisplayRegion[8].y_len));
	v4a_drawrect(&flex_fbdisplay,gui_rect(0,0,vrect_flex.width,vrect_flex.height),ARGB(0,0,0,0));
	
}	
	fclose(HZK);
	fclose(ASCII);

	if((G_DispMode==dmode_180C)||(G_DispMode==dmode_270C))
	{
		unsigned char * g_Buffer180;
		//g_Buffer180 = (unsigned char *)malloc(vrect_flex.height * vrect_flex.width * 4);
		g_Buffer180 = (unsigned char *)userbkgnd_bmp_data;
		memset( g_Buffer180, 0, (vrect_flex.height * vrect_flex.width * 4) );
		char* _g_Buffer = (char*)userbkgnd_bmp_txt;
		char* _g_Buffer1 = g_Buffer180+vrect_flex.height * vrect_flex.width * 4;
		for ( j = 0; j < vrect_flex.height * vrect_flex.width; j++)
		{
			_g_Buffer1 -= 4;
			memcpy( _g_Buffer1, _g_Buffer, 4 );
			_g_Buffer  += 4;
		
		}
		_g_Buffer = (char*)userbkgnd_bmp;
		_g_Buffer1 = g_Buffer180;
		memset( _g_Buffer, 0, 1366*768*4 );
		memcpy( _g_Buffer, _g_Buffer1, vrect_flex.height * vrect_flex.width * 4 );

		free( (unsigned char *)g_Buffer180 );
	}



	v4a_fillvram(flex_fbdisplay.img_base,vrect_flex.width*vrect_flex.height,(int *)userbkgnd_bmp);
	v4a_displayflex(1);
//2 up
//0 left
//3 down
//1 right
	if(G_DispMode==dmode_0C)
		v4a_setautoscroll(1,0,G_LFTTXT_vf_speed,1);
		
	else if(G_DispMode==dmode_90C)
		v4a_setautoscroll(1,2,G_LFTTXT_vf_speed,1);
	else if(G_DispMode==dmode_180C)
		v4a_setautoscroll(1,1,G_LFTTXT_vf_speed,1);
	else if(G_DispMode==dmode_270C)
		v4a_setautoscroll(1,3,G_LFTTXT_vf_speed,1);
	return 0;

}


extern char program_list[200][40];
extern int program_list_num;
extern int Emergency_Flag;
extern int Emergency_Flag_Last;
extern int AF_FLAG;

int display_picture_in_video(char state_num0)
{
	GUIPOINT dp;
	DIR *dir = NULL;
	struct dirent *ptr = NULL;
	int i = 0,m=0,j=0;
	int sum_num;
	static int picture_num =0;
	char pic[40][40];
	char picture_full_name[64];
	
	static int bkg_flag = 0;
	
	static int ad_play_num = 0;
	
	int ad_stay_time = 0;
	
	
	int N = 10;
	
	int x0=0,y0=0,x1=0,y1=0,w=0,h=0;
	
	srand((unsigned)time(NULL)); //产生随机种子

	//if((DisplayRegion[9].x_len <=1000) && (DisplayRegion[9].y_len <=600))
	//{
		//return 0;
	//}

	memset(pic[0],0,40);

	if(Emergency_Flag)
	{
		if(Emergency_Flag_Last==0)
		{
			picture_num = 0;
			ad_play_num = 0;
			Emergency_Flag_Last=Emergency_Flag;
			system("killall mplayer");
		}
		Emergency_Flag_Last=Emergency_Flag;
		if(picture_num >= 6)
			picture_num = 0;
			
		memset(picture_full_name,0,64);
		
		strcpy(picture_full_name,"/v4asys/area0/");
		strcat(picture_full_name,Appease_picture[picture_num]);
		
		ad_play_num++;
		if(ad_play_num>=15)
		{
			ad_play_num = 0;
			picture_num++;
			//return 0;
		}
		else if(ad_play_num !=1)
			return 0;
		else
			;
		
		if(picture_num >= 6)
			picture_num = 0;

	}
	else
	{
		//printf("program_list_num = %d\n", program_list_num);
		
		i=0;
		bkg_flag = 1;
		if(program_list_num == 0)
				picture_num = 0;
		for(i=0;i<program_list_num;i++)
		{
			if( media_is_valid(program_list[i]) != 0 )
			{
			bkg_flag=0;
			break;	
			}	
		}
		
			
		if(Emergency_Flag_Last)
		{
			picture_num = 0;
			ad_play_num = 0;
			
			if(Emergency_Flag_Last==1)
			{
				AF_FLAG = 1;
				system("killall mplayer");
			}
				
			Emergency_Flag_Last=0;
		}
		Emergency_Flag_Last=Emergency_Flag;	
		memset(picture_full_name,0,64);
		if(bkg_flag)
		{
			strcpy(picture_full_name,"/v4asys/area0/bkg.jpg");
			ad_play_num++;
			if(ad_play_num>=10)
			{
				ad_play_num = 0;
				return 0;
			}
			else if(ad_play_num !=1)
				return 0;
			else
				;
			
			
		}
		else
		{
			
			if(picture_num >= program_list_num)
				picture_num = 0;
			
			
			while( (ad_stay_time=media_is_valid(program_list[picture_num])) == 0 )
			{
				if(program_list_num == 0)
					return 0;
				picture_num++;
				
				if(picture_num >= program_list_num)
				{
					picture_num = 0;			
						return 0;
				}
			}
			printf("%s stay_time = %d\n",program_list[picture_num],ad_stay_time);
			strcpy(picture_full_name,"/v4asys/area1/");
			
			strcat(picture_full_name,program_list[picture_num]);
			
			//printf("program_list[%d] = %s\n",picture_num,picture_full_name);
			
			ad_play_num++;
			if(ad_play_num>=((ad_stay_time-1)*2))
			{
				ad_play_num = 0;
				picture_num++;
				return 0;
			}
			else if(ad_play_num !=1)
				return 0;
			else
				;
			if(picture_num >= program_list_num)
				picture_num = 0;
			
		}
	}
	FILE *pictu = fopen(picture_full_name,"r");	
	if(pictu==NULL)
	{		
		return 0;
	}
	else
		fclose(pictu);
		
	userbkgnd_pic.img_base = bkgnd_fbcopy.img_base;
	userbkgnd_pic.img_xlen = DisplayRegion[9].x_len;
	userbkgnd_pic.img_ylen = DisplayRegion[9].y_len;
	userbkgnd_pic.img_bpp = 16;

	userbkgnd_pic0.img_base = V4A_BKGNDBASE0;
	userbkgnd_pic0.img_xlen = GUI_LCM_XMAX;
	userbkgnd_pic0.img_ylen = GUI_LCM_YMAX;
	userbkgnd_pic0.img_bpp = 16;
	
	v4a_drawrect(&userbkgnd_pic,gui_rect(0,0,userbkgnd_pic.img_xlen,userbkgnd_pic.img_xlen),ARGB(0,0,0,0));


	if((G_DispMode == dmode_180C) || (G_DispMode == dmode_270C))
	{
		if( 1==jpg_bmp_display(picture_full_name, DisplayRegion[9].x_len, DisplayRegion[9].y_len, 1) )
			return 1;
	}
	else
	{
		if( 1==jpg_bmp_display(picture_full_name, DisplayRegion[9].x_len, DisplayRegion[9].y_len, 0) )
			return 1;
	}


	v4a_fillvram(userbkgnd_pic.img_base,DisplayRegion[9].x_len*DisplayRegion[9].y_len/2,(int *)userbkgnd_bmp_data);
	
	//switch(rand()%6)
	switch(0)
	{
		case 1://从中心向四周逐次显示
		for(i=0; i<N/2-1; i++)
		{
			x0 = DisplayRegion[9].x_len/N *(N/2-1-i);
			y0 = DisplayRegion[9].y_len/N *(N/2-1-i);
			x1 = DisplayRegion[9].x_len/N *(N/2-1-i);
			y1 = DisplayRegion[9].y_len/N *(N/2-1-i);
			w = DisplayRegion[9].x_len/N *2*(i+1);
			h = DisplayRegion[9].y_len/N *2*(i+1);
		
			if(w%2==1)
				w=w+1;
				
			v4a_copyimagecut(&userbkgnd_pic,&userbkgnd_pic0,gui_rect(x0,y0,w,h),gui_point(DisplayRegion[9].s_x+x1, DisplayRegion[9].s_y+y1));
			
			usleep(1000*50);
		}
		break;
		
		case 2://水平百叶窗显示
		for(i=0; i<DisplayRegion[9].y_len/N-1; i++)
		{
		
			for ( j = 0; j < N; j++ )
			{	
				x0 = 0;
				y0 = DisplayRegion[9].y_len/N*j+i;
				x1 = 0;
				y1 = DisplayRegion[9].y_len/N*j+i;
				
				w = DisplayRegion[9].x_len;
				h = 1;
				
				if(w%2==1)
				w=w+1;
				
				v4a_copyimagecut(&userbkgnd_pic,&userbkgnd_pic0,gui_rect(x0,y0,w,h),gui_point(DisplayRegion[9].s_x+x1, DisplayRegion[9].s_y+y1));		
				//v4a_copyimagecut(&userbkgnd_pic,&userbkgnd_pic0,gui_rect(0,DisplayRegion[9].y_len/10*j+i,DisplayRegion[9].x_len,1),gui_point(DisplayRegion[9].s_x, DisplayRegion[9].s_y+DisplayRegion[9].y_len/10*j+i));
			}
			usleep(1000);
		}
		break;
		
		case 3://垂直百叶窗显示
		for(i=0; i<DisplayRegion[9].x_len/(2*N)-1; i++)
		{			
			
			for ( j = 0; j < N; j++ )
			{	
			
				x0 = DisplayRegion[9].x_len/N*j+2*i;
				y0 = 0;
				x1 = DisplayRegion[9].x_len/N*j+2*i;
				y1 = 0;
				w = 2;
				h = DisplayRegion[9].y_len;
					
				v4a_copyimagecut(&userbkgnd_pic,&userbkgnd_pic0,gui_rect(x0,y0,w,h),gui_point(DisplayRegion[9].s_x+x1, DisplayRegion[9].s_y+y1));
			}
			usleep(2000);
		}
		break;
		
		case 4://从右下角飞入
		for ( j = 0; j < N-1; j++ )
		{
			x0 = 0;
			y0 = 0;
			x1 = DisplayRegion[9].x_len/N*(N-j-1);
			y1 = DisplayRegion[9].y_len/N*(N-j-1);
			
			w = DisplayRegion[9].x_len/N*(j+1);
			h = DisplayRegion[9].y_len/N*(j+1);
			
			if(w%2==1)
				w=w-1;
				
			v4a_copyimagecut(&userbkgnd_pic,&userbkgnd_pic0,gui_rect(x0,y0,w,h),gui_point(DisplayRegion[9].s_x+x1, DisplayRegion[9].s_y+y1));
			usleep(1000*20);
		}
		break;
		
		case 5://从左上角飞入
		for ( j = 0; j < N-1; j++ )
		{
			
			x0 = DisplayRegion[9].x_len/N*(N-j-1);
			y0 = DisplayRegion[9].y_len/N*(N-j-1);
			x1 = 0;
			y1 = 0;
			
			w = DisplayRegion[9].x_len/N*(j+1);
			h = DisplayRegion[9].y_len/N*(j+1);
			
			if(w%2==1)
				w=w+1;
				
			v4a_copyimagecut(&userbkgnd_pic,&userbkgnd_pic0,gui_rect(x0,y0,w,h),gui_point(DisplayRegion[9].s_x+x1, DisplayRegion[9].s_y+y1));
			usleep(1000*20);
		}
		break;
	}
	v4a_copyimage(&userbkgnd_pic,&userbkgnd_pic0,gui_point(DisplayRegion[9].s_x, DisplayRegion[9].s_y));
	memset(picture_full_name,0,64);

	return 0;
}

int display_user_picture()
{
	GUIPOINT dp;
	DIR *dir = NULL;
	struct dirent *ptr = NULL;
	int i = 0,m=0;
	int sum_num;
	static int picture_num =0;
	char pic[40][40];
	char picture_full_name[64];

	if((DisplayRegion[6].x_len == 0) || (DisplayRegion[6].y_len == 0))
	{
		return 0;
	}

	memset(picture_full_name,0,64);
	
	strcpy(picture_full_name, "/v4asys/blue.bmp");

	FILE *pictu = fopen(picture_full_name,"r");
	
	if(!pictu)
	{
		printf("open userbkgnd file failed\n");
		return 1;
	}
	int readlen = 0;
	readlen = fread(userbkgnd_bmp,4*1366*768,1,pictu);

	BMPINFO* userbkgnd_info = bmp_getinfo(userbkgnd_bmp);

	userbkgnd_pic.img_base = bkgnd_fbcopy.img_base;
	userbkgnd_pic.img_xlen = userbkgnd_info->width;
	userbkgnd_pic.img_ylen = userbkgnd_info->height;
	userbkgnd_pic.img_bpp = 32;


	if((DisplayRegion[6].x_len < userbkgnd_pic.img_xlen)||(DisplayRegion[6].y_len < userbkgnd_pic.img_ylen))
		return 0;
	dp.x = (DisplayRegion[6].x_len - userbkgnd_pic.img_xlen)/2;
	dp.y = (DisplayRegion[6].y_len - userbkgnd_pic.img_ylen)/2;

	if((G_DispMode == dmode_180C) || (G_DispMode == dmode_270C))
	{
		dp.x = (DisplayRegion[6].x_len + userbkgnd_pic.img_xlen)/2;
		dp.y = (DisplayRegion[6].y_len + userbkgnd_pic.img_xlen)/2;
	}

	if(G_DispMode == dmode_180C)
	{
		dp.x -= userbkgnd_pic.img_xlen;
	}

	else if(G_DispMode == dmode_270C)
	{
		dp.y -= userbkgnd_pic.img_ylen;
	}

	if(1)
	{	
		if(userbkgnd_info->width*2%4!=0)
			m=4-userbkgnd_info->width*2%4;
		else 
			m=0;
	}
	
	for(i = 0;i<(int)(userbkgnd_info->height);i++)
		memcpy(&userbkgnd_bmp_data[((int)(userbkgnd_info->height)-1-i)*4*(int)(userbkgnd_info->width)],userbkgnd_info->pixel_array+i*((int)(userbkgnd_info->width)*4),(int)(userbkgnd_info->width)*4);
	v4a_fillvram(userbkgnd_pic.img_base,userbkgnd_info->height*userbkgnd_info->width,(int *)userbkgnd_bmp_data);
	v4a_copyimage(&userbkgnd_pic,&overlay_fbdisplay,gui_point(DisplayRegion[6].s_x+0, DisplayRegion[6].s_y+0));
	
	memset(picture_full_name,0,64);
	fclose(pictu);
	return 0;
}

